﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YJKElementFinish.WallInsulationLayer.Controllers;

namespace YJKElementFinish.WallInsulationLayer.Forms
{
	public partial class EditCurveForm : System.Windows.Forms.Form
	{
		public bool PickButton
		{
			get
			{
				return this.m_PickButton;
			}
			set
			{
				this.m_PickButton = value;
			}
		}

		public bool IsContinuous
		{
			get
			{
				return this.chbIsContinuous.Checked;
			}
		}

		public EditCurveForm.OperateType CurrentOperateType
		{
			get
			{
				return this.m_CurrentOperateType;
			}
		}

		public EditCurveForm()
		{
			this.InitializeComponent();
			this.SetToolStripSize();
		}

		private void EditCurveForm_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.ReadData();
		}

		private void EditCurveForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void ChangeOperateState()
		{
			Cursor.Position = new System.Drawing.Point(Cursor.Position.X, base.Bottom + 1);
			this.SendSetfocusMessage();
			this.SimulationPressESC();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "EditCurveForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				this.m_CurrentOperateType = EditCurveForm.OperateType.OT_SelectRefWall;
				this.SetSelectType(this.m_CurrentOperateType);
				this.chbIsContinuous.Checked = true;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			this.chbIsContinuous.Checked = Convert.ToBoolean(streamReader.ReadLine());
			this.m_CurrentOperateType = (EditCurveForm.OperateType)Convert.ToInt32(streamReader.ReadLine());
			this.SetSelectType(this.m_CurrentOperateType);
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "EditCurveForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.chbIsContinuous.Checked);
			streamWriter.WriteLine((int)this.m_CurrentOperateType);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void EditCurveForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SimulationPressESC();
		}

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			EditCurveForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			EditCurveForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SendSetfocusMessage()
		{
			int wMsg = 7;
			EditCurveForm.SendMessage(AddSurfaceLayersController._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		private void SetSelectType(EditCurveForm.OperateType optType)
		{
			if (optType == EditCurveForm.OperateType.OT_DrawLine)
			{
				this.tsbDrawLine.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawLine.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_DrawRect)
			{
				this.tsbDrawRect.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawRect.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_DrawArc3Pt)
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArc3Pt.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_DrawArcCenter)
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbDrawArcCenter.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_SelectRefCurve)
			{
				this.tsbSelectCurve.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectCurve.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_SelectRefWall)
			{
				this.tsbSelectRefWall.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbSelectRefWall.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_Reverse)
			{
				this.tsbReverseOrientation.CheckState = CheckState.Checked;
			}
			else
			{
				this.tsbReverseOrientation.CheckState = CheckState.Unchecked;
			}
			if (optType == EditCurveForm.OperateType.OT_Delete)
			{
				this.tsbDelete.CheckState = CheckState.Checked;
				return;
			}
			this.tsbDelete.CheckState = CheckState.Unchecked;
		}

		private void SetToolStripSize()
		{
			double num = 1.0;
			double num2 = 1.0;
			WinDPIManager.GetWinFormXYDpi(ref num, ref num2);
			int num3 = (int)(41.0 + (double)this.tsbDrawLine.Size.Width * (num - 1.0));
			int num4 = (int)((double)this.tsbDrawLine.Size.Width * num);
			int height = (int)((double)this.tsbDrawLine.Size.Height * num2);
			this.tsbDrawLine.Size = new Size(num4, height);
			this.tsbDrawRect.Size = new Size(num4, height);
			this.tsbDrawArcCenter.Size = new Size(num4, height);
			this.tsbDrawArc3Pt.Size = new Size(num4, height);
			this.tsbSelectCurve.Size = new Size(num4, height);
			this.tsbSelectRefWall.Size = new Size(num4, height);
			this.tsbReverseOrientation.Size = new Size(num4, height);
			this.tsbDelete.Size = new Size(num4, height);
			this.tsbDrawFinish.Size = new Size(num4, height);
			int width = (int)((double)this.toolStripEdit.ImageScalingSize.Width * num);
			int height2 = (int)((double)this.toolStripEdit.ImageScalingSize.Height * num2);
			this.toolStripEdit.ImageScalingSize = new Size(width, height2);
			this.toolStripEdit.Size = new Size(num4 * 9 + num3, this.toolStripEdit.Size.Height);
			base.Size = new Size(this.chbIsContinuous.Size.Width + this.toolStripEdit.Size.Width, base.Size.Height);
		}

		private void EditCurveForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SendSetfocusMessage();
				return;
			}
			base.Focus();
		}

		private void chbIsContinuous_CheckedChanged(object sender, EventArgs e)
		{
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawLine_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_DrawLine;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = true;
			this.ChangeOperateState();
		}

		private void tsbDrawArc3Pt_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_DrawArc3Pt;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = true;
			this.ChangeOperateState();
		}

		private void tsbDrawRect_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_DrawRect;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbDrawArcCenter_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_DrawArcCenter;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbSelectCurve_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_SelectRefCurve;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbSelectRefWall_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_SelectRefWall;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbReverseOrientation_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_Reverse;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.chbIsContinuous.Enabled = false;
			this.ChangeOperateState();
		}

		private void tsbDelete_Click(object sender, EventArgs e)
		{
			this.m_CurrentOperateType = EditCurveForm.OperateType.OT_Delete;
			this.SetSelectType(this.m_CurrentOperateType);
			this.m_PickButton = true;
			this.ChangeOperateState();
		}

		private void tsbDrawFinish_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.OK;
			this.PickButton = false;
			base.Close();
		}

		private void tsbDrawLine_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawRect_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArc3Pt_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDrawArcCenter_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectCurve_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbReverseOrientation_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbDelete_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private void tsbSelectRefWall_MouseEnter(object sender, EventArgs e)
		{
			base.Focus();
		}

		private static int m_Version = 1;

		private EditCurveForm.OperateType m_CurrentOperateType = EditCurveForm.OperateType.OT_SelectRefWall;

		private bool m_PickButton;

		public enum OperateType
		{
			OT_DrawLine = 1,
			OT_DrawRect,
			OT_DrawArc3Pt,
			OT_DrawArcCenter,
			OT_SelectRefCurve,
			OT_SelectRefWall,
			OT_Reverse,
			OT_Delete
		}
	}
}
